<html>
<head>
	<title>Foregroundcolor</title>
	<link type='text/css' rel='stylesheet' href='./style1.css'/>
</head>
<script language='JavaScript' src='./foregroundcolor.js'></script>
<body onLoad="init();">

<span class="title">Foregroundcolor</span>
<br><br>

<?php

	include('./config/dbconnect.inc');

	echo '<table border="0" cellspacing="0" cellpadding="0">';
		echo '<tr valign="top">';
			echo '<td>';

			echo 'predefined colors<br><br>';

			$color_id = 0;

			echo '<table border="0" cellspacing="0" cellpadding="3">';
				addColor(255, 0, 0);
				addColor(255, 127, 0);
				addColor(255, 255, 0);
				addColor(127, 255, 0);
				addColor(0, 255, 0);
				addColor(0, 255, 127);
				addColor(0, 255, 255);
				addColor(0, 127, 255);
				addColor(0, 0, 255);
				addColor(127, 0, 255);
				addColor(255, 0, 255);
				addColor(255, 0, 127);
				addColor(255, 255, 255);
				addColor(127, 127, 127);
				addColor(0, 0, 0);
			echo '</table>';

			echo '</td>';
			echo '<td>';

			echo 'userdefined colors<br><br>';

			echo '<table border="0" cellspacing="0" cellpadding="3">';
			
				$hasColors = false;
				
				$sql = 'select * from usercolors order by usercolor_id';
				$result = mysql_query($sql);
				while($row = mysql_fetch_array($result))
				{
					$red = $row['usercolor_red'];
					$green = $row['usercolor_green'];
					$blue = $row['usercolor_blue'];

					addColor($red, $green, $blue);
					$hasColors = true;
				}
				
				/*$address = getcwd()."/filesystem/editor/usercolors.txt";
				$filehandler = fopen($address, "r");
				$hasColors = false;

				if(filesize($address)>0)
				{
					$content = fread($filehandler, filesize($address));
					$line = "";

					while(strlen($content)-strlen($line) > 0)
					{

						$line = strtok($content, ';');
						$content = substr($content, strlen($line)+1, strlen($content)-strlen($line));

						$red = strtok($line, ',');
						$line = substr($line, strlen($red)+1, strlen($line)-strlen($red));

						$green = strtok($line, ',');
						$line = substr($line, strlen($green)+1, strlen($line)-strlen($green));

						$blue = strtok($line, ';');
						$line = substr($line, strlen($blue)+1, strlen($line)-strlen($blue));
						
						if($red!='' && $green!='' && $blue!='')
						{
							addColor($red, $green, $blue);
							$hasColors = true;
						}
					}
				}
				fclose($filehandler);*/

			echo '</table>';

			if($hasColors)
				echo '<br>';

			echo '<a href="javaScript:openNewColor();">new color</a>';
			echo '<br><br>';
			echo '<a href="javaScript:deleteColors();">delete userdefined colors</a>';
			echo '<br>';

			echo '</td>';
		echo '</tr>';
	echo '</table>';

	function addColor($red, $green, $blue)
	{
		global $color_id;
		$color_id++;

		echo '<tr onMouseOver="mouseIn(\'color'.$color_id.'L\', \'color'.$color_id.'R\');" onMouseOut="mouseOut(\'color'.$color_id.'L\', \'color'.$color_id.'R\');" onClick="changeColor('.$red.','.$green.','.$blue.')">';
			echo '<td id="color'.$color_id.'L" width="50" background="./images/colorchooser_bg1.png">';
				echo '<table border="1" cellspacing="0" cellpadding="0"><tr><td bgcolor="'.getColor($red, $green, $blue).'"><img src="./images/1x1.gif" width="30" height="12");"/></td></tr></table>';
			echo '</td>';
			echo '<td id="color'.$color_id.'R" width="150" background="./images/colorchooser_bg2.png">';
				echo '#'.getColor($red, $green, $blue);
			echo '</td>';
		echo '</tr>';
		echo '<tr><td><img src="./images/1x1.gif" height="1"/></td><td></td><td></td></tr>';
	}

	function getColor($red, $green, $blue)
	{
		$red = round($red, 0);
		$green = round($green, 0);
		$blue = round($blue, 0);

		$color = getHex($red).getHex($green).getHex($blue);
		return $color;
	}

	function getHex($int)
	{
		$char[0] = '0';
		$char[1] = '1';
		$char[2] = '2';
		$char[3] = '3';
		$char[4] = '4';
		$char[5] = '5';
		$char[6] = '6';
		$char[7] = '7';
		$char[8] = '8';
		$char[9] = '9';
		$char[10] = 'A';
		$char[11] = 'B';
		$char[12] = 'C';
		$char[13] = 'D';
		$char[14] = 'E';
		$char[15] = 'F';

		$left = 0;
		$right = 0;

		while((16*$left+$right) < $int)
		{
			$right++;
			if($right > 15)
			{
				$left++;
				$right = 0;
			}
		}

		return $char[$left].$char[$right];
	}


?>

</body>
</html>

